#include <QPainter>
#include <QKeyEvent>
#include "qrect.h"
#include "qtimer.h"
#include <QDebug>
#include <QFont>
#include <playwindow.h>
#include <snake.h>
#include <QRandomGenerator>

PlayWindow::PlayWindow(QWidget *parent)
    : QMainWindow{parent},blsRun(false),speed(500)
{
    this->setFixedSize(960,540);

    // 设置图标
    this->setWindowIcon(QPixmap(":/res/Snake_window.png"));
    // 设置标题
    this->setWindowTitle("贪吃蛇");

}


void PlayWindow::paintEvent(QPaintEvent *)
{
    // 加载背景
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/Snake_window.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);

    if(!blsRun)
    {
        //初始化蛇
        InitSnake();
    }

    //外墙
    painter.setPen(Qt::black);
    painter.drawRect(15,15,930,510);
    //内墙
    painter.setPen(Qt::black);
    painter.drawRect(20,20,920,500);

    //画蛇
    painter.setPen(Qt::black);
    painter.setBrush(Qt::green);
    painter.drawPixmap(food,QPixmap(":/res/Snake_food.png"));

    painter.drawPixmap(vSnakeRect[0],QPixmap(":/res/Snake_head.png"));
    for(int i=1;i<vSnakeRect.size();i++)
    {
        painter.drawPixmap(vSnakeRect[i],QPixmap(":/res/Snake_body.png"));
    }

    if(blsOver)
        timer->stop();


    //显示游戏开始与结束
    QFont font1("Courier",24);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(400,250,sDisplay);

    //得分
    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(22,40,scoreLabel);
    painter.drawText(112,40,QString::number(nScore));


}

void PlayWindow::InitSnake()
{
    nDirection=2;
    blsRun=true;
    blsOver=false;
    scoreLabel="得分：";
    nScore=0;
    food=CreateRect();
    vSnakeRect.resize(8);
    for(int i=0;i<vSnakeRect.size();i++)
    {
        QRect rect(100,70+10*i,10,10);
        vSnakeRect[vSnakeRect.size()-1-i]=rect;
    }
    SnakeHead=vSnakeRect.first();
    timer=new QTimer(this);
    timer->start(speed);
    connect(timer, &QTimer::timeout, this, &PlayWindow::Snake_update);

}


void PlayWindow::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *key=(QKeyEvent*)event;

    switch (key->key())
    {
        case Qt::Key_Up:nDirection=1;
            break;
        case Qt::Key_Down:nDirection=2;
            break;
        case Qt::Key_Left:nDirection=3;
            break;
        case Qt::Key_Right:nDirection=4;
            break;
        default:;
    }
    update();
}

void PlayWindow::Snake_update(){
    sDisplay="";
    SnakeHead=vSnakeRect.first();
    IsEat();
    IsHit();
    for(int j=0;j<vSnakeRect.size()-1;j++)
    {
        vSnakeRect[vSnakeRect.size()-1-j]=vSnakeRect[vSnakeRect.size()-2-j];
    }

    switch(nDirection)
    {
    case 1:
        SnakeHead.setTop(SnakeHead.top()-10);
        SnakeHead.setBottom(SnakeHead.bottom()-10);
        break;
    case 2:
        SnakeHead.setTop(SnakeHead.top()+10);
        SnakeHead.setBottom(SnakeHead.bottom()+10);
        break;
    case 3:
        SnakeHead.setLeft(SnakeHead.left()-10);
        SnakeHead.setRight(SnakeHead.right()-10);
        break;
    case 4:
        SnakeHead.setLeft(SnakeHead.left()+10);
        SnakeHead.setRight(SnakeHead.right()+10);
    default:;
    }

    vSnakeRect[0]=SnakeHead;
    if(SnakeHead.left()<20||SnakeHead.right()>940||SnakeHead.top()<20 || SnakeHead.bottom()>520)
    {
        sDisplay="游戏结束";
        blsOver=true;
    }
    update();
}

QRect PlayWindow::CreateRect()
{
    int x,y;
    x = QRandomGenerator::global()->bounded(25);
    y = QRandomGenerator::global()->bounded(30);
    QRect rect(20+x*10,20+y*10,10,10);
    return rect;
}


void PlayWindow::IsEat(){
    if(SnakeHead==food)
    {
        SnakeHead=food;
        vSnakeRect.push_back(vSnakeRect.last());
        food=CreateRect();
        nScore+=10;
        if(speed>50)
        {
            speed=speed-10;
            timer->stop();
            timer->start(speed);
        }
    }

}


void PlayWindow::IsHit(){
    for(int i=1;i<vSnakeRect.size();i++)
    {
        if(SnakeHead==vSnakeRect[i])
        {
            sDisplay="游戏结束";
            blsOver=true;
            update();
        }
    }
}



